<?php

namespace app\admin\controller;

use app\admin\model\Category;
use app\admin\model\Article as ArticleModel;

class Article extends AdminBase
{
    public function articleList()
    {
        $data = model('article')->getArticles(10);

        $this->assign("data", $data['data']);
        $this->assign("page", $data['page']);
        $this->assign("domain", config('qiniu')['domain']);
        $this->assign("keywords", input('param.keywords', ''));
        return $this->fetch();
    }

    /**
     * @Title: add
     * @Description: 文章添加
     * @return mixed|void
     * @Author: 李军伟
     */
    public function add()
    {
        if (request()->isPost()) {
            $input = input();
            if (empty($input['title'])) {
                $this->error('名称不可为空');
            }
            if (empty($input['content'])) {
                $this->error('内容不可为空');
            }
            if (empty($input['introduction'])) {
                $this->error('简介不可为空');
            }
            if (empty($input['author'])) {
                $this->error('作者不可为空');
            }
            $insert_data = array(
                'title' => $input['title'],
                'content' => $input['content'],
                'introduction' => $input['introduction'],
                'author' => $input['author'],
                'language' => $input['language'],
                'add_time' => time(),
            );
            $res = model('article')->insertGetId($insert_data);
            if ($res > 0) {
                $this->success('添加成功', 'articleList');
            } else {
                $this->error('添加失败，请重试');
            }
        } else {
            return $this->fetch();
        }
    }

    /**
     * @Title: del
     * @Description: 文章删除
     * @return mixed
     * @Author: 李军伟
     */
    public function del()
    {
        if (request()->isPost()) {
            $article_id = input("param.id");
            // 判断是否为视频&是否与题目绑定
            $article = model('article')->getArticle(['id' => $article_id]);
            if ($article['']) {

            }
            $info = model('article')->delWen($article_id);
            if ($info > 0) {
                $this->success('删除成功', 'getList');
            } else {
                $this->error('删除失败，请重试');
            }
        }
    }

    /**
     * @Title: update
     * @Description: 文章更新
     * @return mixed|void
     * @Author: 李军伟
     */
    public function update()
    {
        if (request()->isPost()) {
            $input = input();
            if (empty($input['title'])) {
                $this->error('名称不可为空');
            }
            if (empty($input['content'])) {
                $this->error('内容不可为空');
            }
            if (empty($input['introduction'])) {
                $this->error('简介不可为空');
            }
            if (empty($input['author'])) {
                $this->error('作者不可为空');
            }
            $list = array(
                'id' => $input['id'],
                'title' => $input['title'],
                'content' => $input['content'],
                'introduction' => $input['introduction'],
                'author' => $input['author'],
                'language' => $input['language'],
                'updated_time' => time()
            );
            $res = model('article')->editArticle($list);
            if ($res > 0) {
                $this->success('更新成功', 'articleList');
            } else {
                $this->error('更新失败，请重试');
            }
        } else {
            //加载页面
            $input = input();
            $where = ['id' => $input['id']];
            $info = (new ArticleModel())->getOne($where);
            $this->assign("domain", config('qiniu')['domain']);
            $this->assign("data", $info);
            return $this->fetch();
        }
    }
}
